package com.linkoog.devtools.http.toolwindow.workbenche.left;

import com.intellij.openapi.Disposable;
import com.intellij.openapi.project.Project;
import com.intellij.ui.Gray;
import com.intellij.ui.JBColor;
import com.intellij.ui.components.JBPanel;
import com.intellij.ui.tabs.JBTabsPosition;
import com.intellij.ui.tabs.TabInfo;
import com.intellij.ui.tabs.impl.JBEditorTabs;
import com.intellij.util.ui.JBUI;
import com.linkoog.devtools.http.toolwindow.workbenche.MessageBoard;
import com.linkoog.devtools.http.toolwindow.workbenche.left.http_service.HttpServicePanel;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;
import javax.swing.tree.TreeNode;
import java.awt.*;
import java.util.function.BiConsumer;
import java.util.function.Consumer;

public class HttpWorkbencheTestLeftPanel extends JPanel implements Disposable{

    private final Project project;
    private final MessageBoard messageBoard;

    private JBEditorTabs editorTabs;

    private HttpServicePanel httpServicePanel;
    private TabInfo httpServiceTabInfo;
    private TabInfo httpFavoritesTabInfo;
    private TabInfo httpHistoryTabInfo;


    public HttpWorkbencheTestLeftPanel(Project project, MessageBoard messageBoard) {
        super(new BorderLayout());
        this.project = project;
        this.messageBoard = messageBoard;
        this.editorTabs = new JBEditorTabs(project, this);
        this.editorTabs.setTabsPosition(JBTabsPosition.top);
        //this.editorTabs.setHideTabs(true);
        //this.editorTabs.setHideTopPanel(true);
        this.add(editorTabs, BorderLayout.CENTER);
        this.setBorder(JBUI.Borders.customLine(new JBColor(Gray._214, new Color(30, 31, 34))));

        initHttpServicePanel();
        initHttpFavoritesPanel();
        initHttpHistoryPanel();
    }

    public void prepareAndLoadData(){
        HttpServicePanel httpServicePanel = (HttpServicePanel)httpServiceTabInfo.getComponent();
        httpServicePanel.loadHttpServiceTreeData();
    }

    public void setHttpServiceChooseListener(BiConsumer<JTree, TreeNode> chooseListener){
        httpServicePanel.setChooseListener(chooseListener);
    }



    private void initHttpServicePanel() {
        httpServicePanel = new HttpServicePanel(project, this);
        httpServiceTabInfo = new TabInfo(httpServicePanel);
        httpServiceTabInfo.setText("接口列表");
        httpServiceTabInfo.setObject("接口列表");
        editorTabs.addTab(httpServiceTabInfo);
    }

    private void initHttpFavoritesPanel() {
        httpFavoritesTabInfo = new TabInfo(new JPanel());
        httpFavoritesTabInfo.setText("收藏列表");
        httpFavoritesTabInfo.setObject("收藏列表");
        editorTabs.addTab(httpFavoritesTabInfo);
    }

    private void initHttpHistoryPanel() {
        httpHistoryTabInfo = new TabInfo(new JPanel());
        httpHistoryTabInfo.setText("历史记录");
        httpHistoryTabInfo.setObject("历史记录");
        editorTabs.addTab(httpHistoryTabInfo);
    }




    @Override
    public void dispose() {
    }
}
